package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.VehicleBringScanCodeConverter;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.ScanCodeTypeEnum;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.enums.VehicleBringScanTypeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.warehouse.VehicleBringGoodsMapper;
import com.quad.innovators.salesease.mapper.warehouse.VehicleBringScanCodeMapper;
import com.quad.innovators.salesease.model.entity.warehouse.VehicleBringGoods;
import com.quad.innovators.salesease.model.entity.warehouse.VehicleBringScanCode;
import com.quad.innovators.salesease.model.form.warehouse.VehicleBringScanCodeForm;
import com.quad.innovators.salesease.service.warehouse.VehicleBringGoodsService;
import com.quad.innovators.salesease.service.warehouse.VehicleBringScanCodeService;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2025/4/3 09:50
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class VehicleBringScanCodeServiceImpl extends ServiceImpl<VehicleBringScanCodeMapper, VehicleBringScanCode> implements VehicleBringScanCodeService {

    private final VehicleBringScanCodeConverter vehicleBringScanCodeConverter;

    private final VehicleBringGoodsMapper vehicleBringGoodsMapper;

    @Override
    public List<VehicleBringScanCodeForm> getListByVehicleCode(String vehicleBringGoodsCode) {
        List<VehicleBringScanCode> vehicleBringScanCodeList = this.list(new QueryWrapper<VehicleBringScanCode>().lambda().eq(VehicleBringScanCode::getVehicleBringGoodsCode, vehicleBringGoodsCode));
        return vehicleBringScanCodeList.stream().map(vehicleBringScanCodeConverter::toForm).toList();
    }

    @Override
    public List<VehicleBringScanCodeForm> getListByVehicleCode(String vehicleBringGoodsCode, Integer scanType) {
        List<VehicleBringScanCode> vehicleBringScanCodeList = this.list(new QueryWrapper<VehicleBringScanCode>().lambda()
                .eq(VehicleBringScanCode::getVehicleBringGoodsCode, vehicleBringGoodsCode).eq(VehicleBringScanCode::getScanType, scanType));
        return vehicleBringScanCodeList.stream().map(vehicleBringScanCodeConverter::toForm).toList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String save(VehicleBringScanCodeForm form) {
        VehicleBringScanCode entity = vehicleBringScanCodeConverter.toEntity(form);
        entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.VEHICLE_BRING_SCAN_CODE));
        VehicleBringGoods vehicleBringGoods = vehicleBringGoodsMapper.selectOne(new QueryWrapper<VehicleBringGoods>().lambda().eq(VehicleBringGoods::getCode, form.getVehicleBringGoodsCode()));
        if (ObjectUtils.isEmpty(vehicleBringGoods)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        if (VehicleBringScanTypeEnum.SHIPPING.getCode().equals(form.getScanType())) {
            vehicleBringGoods.setSaleNumber(vehicleBringGoods.getSaleNumber() + form.getProductNumber());
        } else if (VehicleBringScanTypeEnum.RETURN.getCode().equals(form.getScanType())) {
            vehicleBringGoods.setReturnNumber(vehicleBringGoods.getReturnNumber() + form.getProductNumber());
        }
        vehicleBringGoodsMapper.updateById(vehicleBringGoods);
        boolean save = this.save(entity);
        return save ? entity.getCode() : "";
    }

    @Override
    public boolean delete(String code) {
        VehicleBringScanCode vehicleBringScanCode = this.getOne(new QueryWrapper<VehicleBringScanCode>().lambda().eq(VehicleBringScanCode::getCode, code));
        if (ObjectUtils.isEmpty(vehicleBringScanCode)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        VehicleBringGoods vehicleBringGoods = vehicleBringGoodsMapper.selectOne(new QueryWrapper<VehicleBringGoods>().lambda().eq(VehicleBringGoods::getCode, vehicleBringScanCode.getVehicleBringGoodsCode()));
        if (ObjectUtils.isEmpty(vehicleBringGoods)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        if (VehicleBringScanTypeEnum.SHIPPING.getCode().equals(vehicleBringScanCode.getScanType())) {
            vehicleBringGoods.setSaleNumber(vehicleBringGoods.getSaleNumber() - vehicleBringScanCode.getProductNumber());
        } else if (VehicleBringScanTypeEnum.RETURN.getCode().equals(vehicleBringScanCode.getScanType())) {
            vehicleBringGoods.setReturnNumber(vehicleBringGoods.getReturnNumber() - vehicleBringScanCode.getProductNumber());
        }
        return this.removeById(vehicleBringScanCode.getId()) && vehicleBringGoodsMapper.updateById(vehicleBringGoods) > 0;
    }

    @Override
    public VehicleBringScanCodeForm getDetail(String code) {
        VehicleBringScanCode vehicleBringScanCode = this.getOne(new QueryWrapper<VehicleBringScanCode>().lambda().eq(VehicleBringScanCode::getCode, code));
        if (ObjectUtils.isEmpty(vehicleBringScanCode)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return vehicleBringScanCodeConverter.toForm(vehicleBringScanCode);
    }
}
